﻿@{
    ViewBag.Title = "DistributionAction";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    @using (Html.BeginForm("DistributionAction", "Role", FormMethod.Post, new { @class = "form-horizontal", role = "form", id = "postForm", onsubmit = "return doSubmit(this,navTabAjaxDone)" }))
    {
        @Html.Hidden("RoleId", ViewData["RoleId"])
        <div class="form-group">
            <div class="col-sm-12">
                <div id="menu-tree">
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-4">
                <button class="btn btn-primary" type="submit">
                    提交</button>
                <a class="btn btn-default" type="button" href="@Url.Action("Index", "Role")" >取消</a>
            </div>
        </div>
    }
</div>
@section Customer_Css{
    <link href="@Url.Content("~/hplus/js/plugins/jstree-3.1.1/themes/proton/style.min.css")" rel="stylesheet" />
}
@section Customer_Js{
    <script src="@Url.Content("~/hplus/js/plugins/jstree-3.1.1/jstree.min.js")" type="text/javascript"></script>
    <script type="text/javascript">
        function doSubmit(form, callback, confirmMsg) {
            var $form = $(form);
            var roleId = $('#RoleId').val();
            var row = $.jstree.reference('#menu-tree').get_selected(true);
            var uniqueArray = [];
            var currentParents = [];
            var count = row.length;
            for (var i = 0; i < count; i++) {
                uniqueArray.push(row[i].id);
                if (row[i].parents != null) {
                    if (currentParents == null || currentParents.sort().toString() != row[i].parents.sort().toString()) {
                        currentParents = row[i].parents;
                        $.merge(uniqueArray, currentParents);
                    }
                }
            }

            var _submitFn = function () {
                $.ajax({
                    type: form.method || 'POST',
                    url: $form.attr("action"),
                    data: { menuIdList: uniqueArray.join(), roleId: roleId },
                    dataType: "json",
                    cache: false,
                    success: callback || BootStrapUI_Core.ajaxDone,
                    error: BootStrapUI_Core.ajaxError
                });
            }

            if (confirmMsg) {
                parent.layer.confirm(confirmMsg, { title: '提示' }, _submitFn);
            } else {
                _submitFn();
            }

            return false;
        }

        $('#menu-tree').jstree({
            "plugins": ["checkbox"],
            'core': {
                'themes': { 'name': 'proton' },
                'data': {
                    'url': '@Url.Action("AjaxDistributionAction", "Role")?id=@ViewData["RoleId"]'
                }
            }
        });
    </script>
}
